package com.sunwayhorizo.health.business.integration.securityI;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.base.BaseManager;
import com.sunwayhorizo.health.business.dto.patad.empi.PatientBaseInfoDTO;
import com.sunwayhorizo.health.business.dto.registries.DepartmentDTO;
import com.sunwayhorizo.health.business.dto.registries.OrganizationDTO;
import com.sunwayhorizo.health.business.registries.employee.EmployeeManagerLocal;
import com.sunwayhorizo.health.business.registries.organization.DepartmentManagerLocal;
import com.sunwayhorizo.health.business.registries.organization.OrganizationManagerLocal;
import com.sunwayhorizo.health.business.registries.organization.query.DepartmentQueryLocal;
import com.sunwayhorizo.health.business.registries.organization.query.OrganizationQueryLocal;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.Employee;
import com.sunwayhorizo.health.hl7.rim.Role;
import com.sunwayhorizo.health.platform.security.dao.SecurityAccountDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityAccount;

@Name("SecurityInterfaceManager")
@Stateless
public class SecurityInterfaceManager extends BaseManager implements SecurityInterfaceManagerLocal
{

    OrganizationManagerLocal organizationManager = (OrganizationManagerLocal) Component.getInstance("OrganizationManager");
    OrganizationQueryLocal organizationQuery = (OrganizationQueryLocal) Component.getInstance("OrganizationQuery");
    DepartmentManagerLocal departmentManager = (DepartmentManagerLocal) Component.getInstance("DepartmentManager");
    DepartmentQueryLocal departmentQuery = (DepartmentQueryLocal) Component.getInstance("DepartmentQuery");
    EmployeeManagerLocal employeeManager = (EmployeeManagerLocal) Component.getInstance("EmployeeManager");


    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public OrganizationDTO findOrganizationByCode(String orgCode) throws Hl7Exception
    {
        Role orgRole = organizationQuery.findOrgRoleByCode(orgCode);
        OrganizationDTO orgDTO = organizationManager.converOrganizationDtoToOrgRole(orgRole);
        return orgDTO;
    }


    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public DepartmentDTO findDepartmentByCode(String departmentCode) throws Hl7Exception
    {
        Role role = departmentQuery.findOrgRoleByCode(departmentCode);
        DepartmentDTO departDTO = departmentManager.converDepartmentDTOByDeptRole(role);
        return departDTO;
    }


    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public PatientBaseInfoDTO findEmployeeByCode(String empCode) throws Hl7Exception
    {
        Employee empRole = employeeManager.findEmployeeDetail(0l, empCode);
        PatientBaseInfoDTO patientBaseInfo = employeeManager.findPlayEntityByRole(empRole);
        return patientBaseInfo;
    }


    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<DepartmentDTO> findWorkGroupListByEmpCode(String empCode) throws Hl7Exception
    {
        Employee empRole = employeeManager.findEmployeeDetail(0l, empCode);
        List<DepartmentDTO> workGroupList = employeeManager.findWorkGroupByRole(empRole);
        return workGroupList;
    }


    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public String findEmpCodeByAccount(String accountName, String password)
    {
        SecurityAccountDao accountDao = (SecurityAccountDao) Component.getInstance("SecurityAccountDao");
        SecurityAccount account = new SecurityAccount();
        account.setAccountName(accountName);
        account.setPassword(password);

        SecurityAccount sa = accountDao.findSecurityAccountByAccountNameAndPWD(account);
        if (sa != null)
        {
            return sa.getEmpCode();
        }
        else
        {
            log.info("SecurityInterfaceManager-findEmpCodeByAccount-账号有误，请重新确认！");
            return null;
        }

    }
}
